/*
 * CHAT.java
 * 
 * Copyright (c) 2009 Giles Millward (Manigra). All rights reserved.
 * 
 * This file is part of Expression program is undefined on line 8, column 40 in Templates/Licenses/license-gplv3.txt..
 * 
 * Expression program is undefined on line 10, column 19 in Templates/Licenses/license-gplv3.txt. is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Expression program is undefined on line 15, column 19 in Templates/Licenses/license-gplv3.txt. is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Expression program is undefined on line 21, column 30 in Templates/Licenses/license-gplv3.txt..  If not, see <http://www.gnu.org/licenses/>.
 */

package playstyle;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import utility.CUtility;

/**
 *
 * @author Mani
 */
public class CHAT {
    ArrayList<CPartyMember> m_partyMembers = new ArrayList<CPartyMember>();

    public CHAT()
    {
        // Add 4 party members
        m_partyMembers.add(new CPartyMember());
        m_partyMembers.add(new CPartyMember());
        m_partyMembers.add(new CPartyMember());
        m_partyMembers.add(new CPartyMember());
    }

    public void setPartyMember(int p_index, JComboBox p_partyMember,
                    JFormattedTextField p_critsPerSecond)
    {
        // link swing fields to this class
        m_partyMembers.get(p_index).setValues(p_partyMember, p_critsPerSecond);
    }

    public void changePartyMember(int p_index)
    {
        String l_selected = m_partyMembers.get(p_index).m_partyMember.getSelectedItem().toString();        
     }

    public void save()
    {
        String l_string = CUtility.openXMLFileSaveDialog("hat", "Set a filename for this HAT party group");
        if (l_string == null)
        {
            return;
        }

        this.save(l_string);
    }

    public void save(String p_fileName) {
        Document doc = DocumentFactory.getInstance().createDocument();
        Element root = doc.addElement("root");
        this.addXMLData(root);

        if (!p_fileName.endsWith(".xml"))
        {
            p_fileName = p_fileName + ".xml";
        }

        FileOutputStream fos;
        try {
            fos = new FileOutputStream(p_fileName);
            OutputFormat format = OutputFormat.createPrettyPrint();
            XMLWriter writer;
            try {
                writer = new XMLWriter(fos, format);
                try {
                    writer.write(doc);
                    writer.flush();
                    writer.close();
                } catch (IOException ex) {
                    Logger.getLogger(CHAT.class.getName()).log(Level.SEVERE, null, ex);
                }

            } catch (UnsupportedEncodingException ex) {
                Logger.getLogger(CHAT.class.getName()).log(Level.SEVERE, null, ex);
            }

        } catch (FileNotFoundException ex) {
            Logger.getLogger(CHAT.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void addXMLData(Element p_root)
    {
        Element l_hat = p_root.addElement("hat");
        Element l_members = l_hat.addElement("members");
        for (int i = 0; i < this.m_partyMembers.size(); i++)
        {
            this.m_partyMembers.get(i).addXMLData(l_members);
        }
    }

    public void read()
    {
        String l_string = CUtility.openXMLFileOpenDialog("hat", "Choose a HAT party group data file");
        if (l_string == null)
        {
            return;
        }

        this.read(l_string);
    }

    public void read(String l_fileName)
    {
        System.out.println("File String " + l_fileName);

        File l_xml = new File(l_fileName);

        SAXReader l_reader = new SAXReader();
        Document l_doc;
        try {
            l_doc = l_reader.read(l_xml);
            Element l_element = (Element) l_doc.selectSingleNode( "//root/hat" );
            this.parseXMLElement(l_element);
        } catch (DocumentException ex) {
            Logger.getLogger(CHAT.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void parseXMLElement(Element p_element)
    {
        List l_list = p_element.selectNodes("//root/hat/members/party_member");
        int i = 0;
        for (Iterator l_iter = l_list.iterator(); l_iter.hasNext(); ) {
                Element l_element = (Element) l_iter.next();
                this.m_partyMembers.get(i++).parseXMLElement(l_element);
         }
    }


    private class CPartyMember {
        public JComboBox m_partyMember;
        public JFormattedTextField m_critsPerSecond;

        public void setValues(JComboBox p_partyMember,
                    JFormattedTextField p_critsPerSecond)
        {
            m_partyMember = p_partyMember;
            m_critsPerSecond = p_critsPerSecond;
        }

        public void addXMLData(Element p_root) {
            // Add ability data
            p_root.addElement("party_member").
                    addAttribute("class", m_partyMember.getSelectedItem().toString()).
                    addAttribute("cps", m_critsPerSecond.getText());
        }

        public void parseXMLElement(Element p_element)
        {
            // p_element has our item ptr in it.
            this.m_partyMember.setSelectedItem(p_element.attributeValue("class"));
            this.m_critsPerSecond.setText(p_element.attributeValue("cps"));
        }
    }
}
